package cn.com.xy.sms.sdk.ui.popu.util;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;

import com.xy.smartsms.data.PublicInfoData;
import com.xy.smartsms.facade.SmsProcessFacade;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import cn.com.xy.sms.sdk.ui.config.UIConfig;
import cn.com.xy.sms.sdk.ui.menu.PopMenus;
import cn.com.xy.sms.sdk.ui.menu.PopMenusChanel;
import cn.com.xy.sms.sdk.ui.popu.part.BubbleCodeHead;
import cn.com.xy.sms.sdk.ui.popu.part.BubbleCodeHeadCMCC;
import cn.com.xy.sms.sdk.ui.popu.part.UIPart;
import cn.com.xy.sms.sdk.util.JsonUtil;

public class UiPartAction implements UiPartInterface{
    public static final String sUiVersion = "201703232137";
    private static int sSupportState = 6;
    
    public Object doUiAction(int type, Object data){
        switch(type){
            case ViewManger.UIPART_ACTION_SET_BG:
                doSetBG(data);
                break;
            case ViewManger.UIPART_ACTION_INIT_UI_CONFIG:
                doInitUiVersion();
                break;
            case ViewManger.UIPART_ACTION_SHOW_DIALOG:
                return true;
                default:
        }
        return null;
    }

    private void doInitUiVersion() {
        UIConfig.setUIVersion(sUiVersion);
        UIConfig.setUISuportState(sSupportState);
    }

    private void doSetBG(Object data) {
      //do nothing
    }

    @Override
    public Object doUiActionMulti(int type, Object... data) {
        switch(type){
        case ViewManger.UIPART_ACTION_TOP_LOGO_CLICK:
            setLogoClickListener(data);
            break;
        case ViewManger.UIPART_ACTION_NEED_CHANGE_LOGO_FLAG:
            return Boolean.TRUE ;
        case ViewManger.UIPART_ACTION_CHANGE_LOGO_SIZE://get SMARTISAN logo Scale parameter
            return setChangeLogoParameter() ;
        case ViewManger.UIPART_ACTION_LOGO_IMAGE_WIDTH: 
            return new Integer(200) ;
        case ViewManger.UIPART_ACTION_LOGO_IMAGE_HIGHT: 
            return new Integer(200) ;
        case ViewManger.UIPART_ACTION_LOGO_BACKGROUND_GRADUAL_CHANGE:
            return Boolean.TRUE ;
        case ViewManger.UIPART_ACTION_LOGO_USE_MINISTRY_LOGO_FLAG:
            return Boolean.TRUE ;
        case ViewManger.UIPART_ACTION_GET_POPMENU:
            return getPopMenu(data);
        case ViewManger.UIPART_ACTION_DOACTION:
            doAction(data);
            break;
        case ViewManger.UIPART_ACTION_BIND_MENU:
            return bindMenuView(data);
        }
        return null;
    }

    private static void doAction(Object[] data) {
        if(data != null && data.length == 3){
            HashMap<String, Object> extend = (HashMap<String, Object>) data[2];
            JSONObject actionMap = (JSONObject) data[1];
            Activity context = (Activity) data[0];
            
            HashMap<String, String> valueMap = new HashMap<String, String>();
            if (extend != null && !extend.isEmpty()) {
                for (Entry<String, Object> entry : extend.entrySet()) {
                    if (entry.getValue() instanceof String) {
                        valueMap.put(entry.getKey(), (String) entry.getValue());
                    }
                }
            }
            
            String action_data = (String) JsonUtil.getValueFromJsonObject(actionMap, "action_data");
            valueMap.put("ManufactureData",  actionMap.optString("ManufactureData", "XIAOYUAN"));
            SmsProcessFacade.getInstance().processAction(context, action_data, valueMap);
        }
    }

    private Object getPopMenu(Object[] data) {
        if(data != null){
            PopMenus pop = null;
            if(data.length == 4){
                 pop = new PopMenusChanel((Activity)data[0], data[1], (Integer)data[2], (Integer)data[3]);
            }else if(data.length == 6){
                 pop = new PopMenusChanel((Activity)data[0], data[1], (Integer)data[2], (Integer)data[3],(Integer)data[4],(Map<String, String>) data[5]);
            }
            return pop;
        }
        return null;
    }

    private View bindMenuView(Object[] data) {
        if(data != null){
            if(data.length == 4){
                return PopMenusChanel.bindMenuView((View)data[0],(PublicInfoData)data[1],(View.OnClickListener)data[2],(Map<String,Object>)data[3]);
            }
        }
        return null;
    }

    private HashMap<String, Float> setChangeLogoParameter() {
        HashMap<String, Float> hashMap = new HashMap<String, Float>() ;
        hashMap.put("scale_0", 0.9375f) ;
        hashMap.put("scale_1", 0.85f) ;
        hashMap.put("scale_2", 1.03f) ;
        hashMap.put("scale_3", 1.0f) ;
        hashMap.put("scale_4", 0.85f) ;
        return hashMap ;
    }

    private void setLogoClickListener(Object[] data) {
        try{
            if(data == null || data.length < 2){
                return;
            }
            List<UIPart> partList = (List<UIPart>) data[0];
            if(partList == null || partList.size() == 0 || data[1] == null){
                return ;
            }
            
            for(UIPart part : partList){
                if(part instanceof BubbleCodeHead){
                    ((BubbleCodeHead) part).setLogoOnClickListener((OnClickListener) data[1]);
                } else if(part instanceof BubbleCodeHeadCMCC){
                    ((BubbleCodeHeadCMCC) part).setLogoOnClickListener((OnClickListener) data[1]);
                }
            }
        }catch(Throwable ex){}
    }
}
